package mobile.TheOldRepublic;

import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import mobile.TheOldRepublic.util.Constants;
import mobile.TheOldRepublic.web.WebViewActivity;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.explodingzombie.util.RssHandler;
import com.explodingzombie.util.RssItem;
import com.explodingzombie.util.RssParser;

public abstract class FeedView extends ListActivity {
	
	protected String feedUrl;
	protected RssHandler handler = null;
	// Use this to create a weak cache for items to display. At implementation time I'm unsure
	// how useful this will be. LP
	protected WeakReference<List<RssItem>> savedItems = null;

	@Override
    protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rss_layout);
//		list = (ListView)findViewById(R.id.);
	}
	
	public void loadTitle(String titleStr){
		TextView title = (TextView)findViewById(R.id.rssTitle);
		title.setText(titleStr);
	}
	
	/*protected void onPause() {
        super.onPause();

        SharedPreferences.Editor ed = mPrefs.edit();
        ed.putInt("view_mode", mCurViewMode);
        ed.commit();
    }*/
	
	@Override
	protected void onStart()
	{
		super.onStart();
//		int results = 0;
//		pd = ProgressDialog.show(this, "Loading...", "Please wait");
//        pd.setCancelable(true);
//        pd.setMax(20);
		List<RssItem> items = (savedItems != null) ? savedItems.get() : null;
		if(items == null){
			InputStream inStream = null;
			try{
				URL url = new URL(feedUrl);
				URLConnection conn = url.openConnection();
				if(conn instanceof HttpURLConnection){
					if(handler == null)
						handler = new TorRssHandler(this);
//					((TorRssHandler)handler).pd = pd;
					inStream = ((HttpURLConnection)conn).getInputStream();
					items = RssParser.parse(inStream, handler);
				}
			}catch(Exception e){
				Toast.makeText(this, "Unable to access feed at this time.", Toast.LENGTH_LONG).show();
				Log.e(this.getClass().getSimpleName(), "An error occured attaching to rss feed: "+feedUrl);
				e.printStackTrace();
				this.finish();
	//			throw new RuntimeException(ioe);
			}finally{
				if(inStream != null)
					try{ inStream.close(); }catch(Exception e){}
			}
			savedItems = new WeakReference<List<RssItem>>(items);
		}

		try{
			setListAdapter(new RssItemAdapter(this, R.layout.blank_layout, items));
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
//		pd.dismiss();
	}

	@Override 
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		super.onListItemClick(l, v, position, id);
		RssItem item = (RssItem)getListAdapter().getItem(position);
		
		final Context activity = this;
        Intent myIntent = new Intent(activity, WebViewActivity.class);
        myIntent.putExtra(Constants.KEY_URL, item.getUrl());
        activity.startActivity(myIntent);
	}
	
	@Override
	public void onSaveInstanceState(Bundle savedInstanceState){
		// TODO implement saving list data.
		super.onSaveInstanceState(savedInstanceState);
	}
	
	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState){
		// TODO implement saving list data.
		super.onRestoreInstanceState(savedInstanceState);
	}
}
